using huqiang.Core.HGUI;
using System;
using UnityEngine;
using huqiang.UIComposite;
using huqiang.UIEvent;
using huqiang;
using huqiang.Data;
using huqiang.UIModel;

public class DataGridPage:UIPage
{
    //反射UI界面上的物体
    class View
    {
        public DataGrid dataGrid;
        public InputBox DataInput;
    }
    class BoolItem : DataGridItem
    {
        public UIElement Open;
        public UserEvent Back;
    }
    class BoolContext:DataGridItemContext
    {
        public bool Open;
    }
    View view;
    int SelectRow = -1;
    DataGridItem select;
    string[] sex = new string[] { "男","女"};
    public override void Initial(UIElement parent, object dat = null)
    {
        base.Initial(parent, dat);
        view = LoadUI<View>("baseUI", "DataGridPage");//"baseUI"创建的bytes文件名,"page"为创建的页面名
        DataGrid.CursorX =  Resources.Load<Texture2D>("StretchWX");
        DataGrid.CursorY =  Resources.Load<Texture2D>("StretchWY");
        InitialData();
        InitialEvent();
    }
    void InitialData()//自定义模板颜色，DataEditor
    {
        var items = HGUIManager.FindModel("baseUI", "ItemMods");
        DataGridColumn column = new DataGridColumn();
        column.Head = "姓名";
        column.SetItemUpdate<DataGridItem, DataGridItemContext>(NameItemUpdate);
        view.dataGrid.AddColumn(column);
        column = new DataGridColumn();
        column.Head = "年龄";
        column.SetItemUpdate<DataGridItem, DataGridItemContext>(NameItemUpdate);
        view.dataGrid.AddColumn(column);
        column = new DataGridColumn();
        column.Head = "性别";
        //column.mod = HGUIManager.FindChild(items, "SelectItem");
        column.SetItemUpdate<DataGridItem, DataGridItemContext>(SelectItemUpdate);
        view.dataGrid.AddColumn(column);
        column = new DataGridColumn();
        column.Head = "身高";
        column.SetItemUpdate<DataGridItem, DataGridItemContext>(NameItemUpdate);
        view.dataGrid.AddColumn(column);
        column = new DataGridColumn();
        column.Head = "婚否";
        column.mod = HGUIManager.FindChild(items, "Check");
        column.SetItemUpdate<BoolItem, BoolContext>(BoolItemUpdate);
        view.dataGrid.AddColumn(column);
        for (int i = 0; i < 20; i++)
            view.dataGrid.AddRow(
                new DataGridItemContext() { Text = "胡强" },
                new DataGridItemContext() { Text = "28" },
                new DataGridItemContext() { Text = "男" },
                new DataGridItemContext() { Text = "168" },
                new BoolContext() { Open = false }
                );
        view.dataGrid.Refresh();
       
    }
    void InitialEvent()
    {
        view.DataInput.Enity.activeSelf = false;
        view.DataInput.OnSubmit = view.DataInput.OnDone = InputItemDone;
    }
    void InputItemDone(InputBox input)
    {
        string str = input.InputString;
        var item = input.DataContext as DataGridItem;
        switch (item.GridColumn.Head)
        {
            case "年龄":
            case "身高":
                str = CharOperation.GetInt(str);
                if (str == "")
                    str = "0";
                break;
        }
        input.InputString = str;
        item.Text.Text = str;
        item.Context.Text = str;
        input.Enity.SetParent(Main);
        input.Enity.activeSelf = false;
    }
    void NameItemUpdate(DataGridItem item, DataGridItemContext context)
    {
        var ue = item.Item.RegEvent<UserEvent>();
        item.Text.Text = context.Text;
        ue.DataContext = item;
        ue.Click = (o, e) =>
        {
            var i = o.DataContext as DataGridItem;
            SelectRow = i.Row;
            var trans = view.DataInput.Enity;
            trans.SetParent(o.Context);
            trans.localScale = Vector3.one;
            UIElement.Resize(view.DataInput.Enity);
            view.DataInput.InputString = i.Text.Text;
            e.RemoveFocus(o);
            e.AddFocus(view.DataInput.InputEvent);
            view.DataInput.DataContext = o.DataContext;
            view.DataInput.Enity.activeSelf = true;
            view.DataInput.SelectAll();
            Input.imeCompositionMode = IMECompositionMode.On;
        };
    }
    void BoolItemUpdate(BoolItem item, BoolContext context)
    {
        item.Open.activeSelf = context.Open;
        item.Back.DataContext = item;
        item.Back.Click = (o, e) => {
            BoolItem b = o.DataContext as BoolItem;
            var bc = b.Context as BoolContext;
            bc.Open = !bc.Open;
            b.Open.activeSelf = bc.Open;
        };
    }
    void SelectItemUpdate(DataGridItem item, DataGridItemContext context)
    {
        var eve = item.Item.RegEvent<UserEvent>();
        item.Text.TextAnchor = TextAnchor.MiddleCenter;
        item.Text.Text = context.Text;
        eve.DataContext = item;
        eve.Click = (o, e) =>
        {
            select = o.DataContext as DataGridItem;
            UIMenu.PopUpOrDown<SelectMenu>(this, o.Context, sex);
            SelectRow = select.Row;
        };
    }
    public override void Cmd(string cmd, object dat)
    {
        switch(cmd)
        {
            case "sex":
                if (select != null)
                {
                    var str = dat as string;
                    select.Text.Text = str;
                    select.Context.Text = str;
                }
                break;
        }
    }
}

public class SelectMenu : UIMenu
{
    class View
    {
        public ScrollY Scroll;
    }
    class Item
    {
        public UserEvent Image;
        public HText Text;
    }
    View view;
    public override void Initial(UIElement parent, UIBase ui, object obj = null)
    {
        base.Initial(parent, ui, obj);
        view = LoadUI<View>("baseUI", "SelectMenu");
        InitialUI();
    }
    void InitialUI()
    {
        view.Scroll.SetItemUpdate<Item, string>(ItemUpdate);
        view.Scroll.scrollType = ScrollType.None;
    }
    public override void Show(UIBase context, Rect pos, int dic, object obj = null)
    {
        ApplyData(obj);
        base.Show(context, pos, dic, obj);
    }
    void ItemUpdate(Item item, string str, int index)
    {
        item.Image.DataContext = str;
        item.Image.Click = (o, e) => {
            HideMenu();
            UIParent.Cmd("sex", o.DataContext);
        };
        item.Text.Text = str;
    }
    void ApplyData(object dat)
    {
        view.Scroll.BindingData = dat;
        view.Scroll.Refresh();
    }
}
